/************************************************************
 *  * EaseMob CONFIDENTIAL
 * __________________
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of EaseMob Technologies.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from EaseMob Technologies.
 */

#import "CreateGroupViewController.h"
#import "ContactSelectionViewController.h"
#import "EMTextView.h"
#import "TongXunLu.h"
#import "TongXunLuViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface CreateGroupViewController ()<UITextFieldDelegate, UITextViewDelegate, EMChooseViewDelegate>

@property (strong, nonatomic) UIView *switchView;
@property (strong, nonatomic) UIBarButtonItem *rightItem;
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) EMTextView *textView;

@property (nonatomic) BOOL isPublic;
@property (strong, nonatomic) UILabel *groupTypeLabel;//群组类型

@property (nonatomic) BOOL isMemberOn;
@property (strong, nonatomic) UILabel *groupMemberTitleLabel;
@property (strong, nonatomic) UISwitch *groupMemberSwitch;
@property (strong, nonatomic) UILabel *groupMemberLabel;

@end

@implementation CreateGroupViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        //_isPublic = NO;
        //_isMemberOn = NO;
        _isMemberOn=YES;
        _isPublic = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    self.title = @"创建群";
    self.view.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.0];
    
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    addButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [addButton setTitle:NSLocalizedString(@"group.create.addOccupant", @"add members") forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [addButton addTarget:self action:@selector(addContacts:) forControlEvents:UIControlEventTouchUpInside];
    _rightItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
    [self.navigationItem setRightBarButtonItem:_rightItem];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
    [backButton setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    [self.view addSubview:self.textField];
    [self.textField becomeFirstResponder];
    //[self.view addSubview:self.textView];
    //[self.view addSubview:self.switchView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

#pragma mark - getter

- (UITextField *)textField
{
    if (_textField == nil) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 40)];
        _textField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _textField.layer.borderWidth = 0.5;
        _textField.layer.cornerRadius = 3;
        _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
        _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.font = [UIFont systemFontOfSize:15.0];
        _textField.backgroundColor = [UIColor whiteColor];
        _textField.placeholder =  @"请输入群名称";
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.delegate = self;
    }
    
    return _textField;
}
/*
 - (EMTextView *)textView
 {
 if (_textView == nil) {
 _textView = [[EMTextView alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, 80)];
 _textView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
 _textView.layer.borderWidth = 0.5;
 _textView.layer.cornerRadius = 3;
 _textView.font = [UIFont systemFontOfSize:14.0];
 _textView.backgroundColor = [UIColor whiteColor];
 _textView.placeholder = NSLocalizedString(@"group.create.inputDescribe", @"please enter a group description");
 _textView.returnKeyType = UIReturnKeyDone;
 _textView.delegate = self;
 }
 
 return _textView;
 }
 
 - (UIView *)switchView
 {
 if (_switchView == nil) {
 _switchView = [[UIView alloc] initWithFrame:CGRectMake(10, 160, 300, 90)];
 _switchView.backgroundColor = [UIColor clearColor];
 
 CGFloat oY = 0;
 //        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, oY, 100, 35)];
 //        label.backgroundColor = [UIColor clearColor];
 //        label.font = [UIFont systemFontOfSize:14.0];
 //        label.numberOfLines = 2;
 //        label.text = NSLocalizedString(@"group.create.groupPermission", @"group permission");
 //        [_switchView addSubview:label];
 
 //        UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(100, oY, 50, _switchView.frame.size.height)];
 //        [switchControl addTarget:self action:@selector(groupTypeChange:) forControlEvents:UIControlEventValueChanged];
 //        [_switchView addSubview:switchControl];
 
 //        _groupTypeLabel = [[UILabel alloc] initWithFrame:CGRectMake(switchControl.frame.origin.x + switchControl.frame.size.width + 5, oY, 100, 35)];
 //        _groupTypeLabel.backgroundColor = [UIColor clearColor];
 //        _groupTypeLabel.font = [UIFont systemFontOfSize:12.0];
 //        _groupTypeLabel.textColor = [UIColor grayColor];
 //        _groupTypeLabel.text = NSLocalizedString(@"group.create.private", @"private group");
 //        _groupTypeLabel.numberOfLines = 2;
 //        [_switchView addSubview:_groupTypeLabel];
 
 //oY += (35 + 20);
 _groupMemberTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, oY, 100, 35)];
 _groupMemberTitleLabel.font = [UIFont systemFontOfSize:14.0];
 _groupMemberTitleLabel.backgroundColor = [UIColor clearColor];
 _groupMemberTitleLabel.text = NSLocalizedString(@"group.create.occupantPermissions", @"members invite permissions");
 _groupMemberTitleLabel.numberOfLines = 2;
 [_switchView addSubview:_groupMemberTitleLabel];
 
 _groupMemberSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, oY, 50, 35)];
 [_groupMemberSwitch addTarget:self action:@selector(groupMemberChange:) forControlEvents:UIControlEventValueChanged];
 [_switchView addSubview:_groupMemberSwitch];
 
 _groupMemberLabel = [[UILabel alloc] initWithFrame:CGRectMake(_groupMemberSwitch.frame.origin.x + _groupMemberSwitch.frame.size.width + 5, oY, 150, 35)];
 _groupMemberLabel.backgroundColor = [UIColor clearColor];
 _groupMemberLabel.font = [UIFont systemFontOfSize:12.0];
 _groupMemberLabel.textColor = [UIColor grayColor];
 _groupMemberLabel.numberOfLines = 2;
 _groupMemberLabel.text = NSLocalizedString(@"group.create.unallowedOccupantInvite", @"don't allow group members to invite others");
 [_switchView addSubview:_groupMemberLabel];
 }
 
 return _switchView;
 }
 */
#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

#pragma mark - EMChooseViewDelegate

- (void)viewController:(EMChooseViewController *)viewController didFinishSelectedSources:(NSArray *)selectedSources
{
    [self showHudInView:self.view hint:NSLocalizedString(@"group.create.ongoing", @"create a group...")];
    
    NSMutableArray *source = [NSMutableArray array];
    
    for (TongXunLu *buddy in selectedSources) {
        [source addObject:buddy.huanxin_username];
    }
    
    EMGroupStyleSetting *setting = [[EMGroupStyleSetting alloc] init];
    setting.groupStyle = eGroupStyle_PrivateMemberCanInvite;
    setting.groupMaxUsersCount=500;
    __weak CreateGroupViewController *weakSelf = self;
    
    [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:self.textField.text description:nil invitees:source initialWelcomeMessage:nil styleSetting:setting completion:^(EMGroup *group, EMError *error) {
        [weakSelf hideHud];
        if (group && !error) {
            [weakSelf showHint:NSLocalizedString(@"group.create.success", @"create group success")];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else{
            [weakSelf showHint:NSLocalizedString(@"group.create.fail", @"Failed to create a group, please operate again")];
        }
    } onQueue:nil];
}

#pragma mark - action
/*
 - (void)groupTypeChange:(UISwitch *)control
 {
 _isPublic = control.isOn;
 
 [_groupMemberSwitch setOn:NO animated:NO];
 [self groupMemberChange:_groupMemberSwitch];
 
 if (control.isOn) {
 _groupTypeLabel.text = NSLocalizedString(@"group.create.public", @"public group");
 }
 else{
 _groupTypeLabel.text = NSLocalizedString(@"group.create.private", @"private group");
 }
 }
 
 - (void)groupMemberChange:(UISwitch *)control
 {
 if (_isPublic) {
 _groupMemberTitleLabel.text = NSLocalizedString(@"group.create.occupantJoinPermissions", @"members join permissions");
 if(control.isOn)
 {
 _groupMemberLabel.text = NSLocalizedString(@"group.create.open", @"random join");
 }
 else{
 _groupMemberLabel.text = NSLocalizedString(@"group.create.needApply", @"you need administrator agreed to join the group");
 }
 }
 else{
 _groupMemberTitleLabel.text = NSLocalizedString(@"group.create.occupantPermissions", @"members invite permissions");
 if(control.isOn)
 {
 _groupMemberLabel.text = NSLocalizedString(@"group.create.allowedOccupantInvite", @"allows group members to invite others");
 }
 else{
 _groupMemberLabel.text = NSLocalizedString(@"group.create.unallowedOccupantInvite", @"don't allow group members to invite others");
 }
 }
 
 _isMemberOn = control.isOn;
 }
 */
- (void)addContacts:(id)sender
{
    if (self.textField.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"prompt", @"Prompt") message:@"请输入群名称" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    [self.view endEditing:YES];
    
    //    ContactSelectionViewController *selectionController = [[ContactSelectionViewController alloc] init];
    //    selectionController.delegate = self;
    //    [self.navigationController pushViewController:selectionController animated:YES];
    
    TongXunLuViewController *addVC = [[TongXunLuViewController alloc] init];
    addVC.orCreateGroupMember=YES;
    addVC.orRefresh=YES;
    [self.navigationController pushViewController:addVC animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    if (self.orCreateGroup) {
        [self showHudInView:self.view hint:NSLocalizedString(@"group.create.ongoing", @"create a group...")];
        
        NSMutableArray *source = [NSMutableArray array];
        
        for (NSString *buddy in self.memberArray) {
            [source addObject:buddy];
        }
        
        EMGroupStyleSetting *setting = [[EMGroupStyleSetting alloc] init];
        setting.groupStyle = eGroupStyle_PrivateMemberCanInvite;
        
        __weak CreateGroupViewController *weakSelf = self;
        
        [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:self.textField.text description:nil invitees:source initialWelcomeMessage:nil styleSetting:setting completion:^(EMGroup *group, EMError *error) {
            [weakSelf hideHud];
            if (group && !error) {
                [weakSelf showHint:NSLocalizedString(@"group.create.success", @"create group success")];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
            else{
                [weakSelf showHint:NSLocalizedString(@"group.create.fail", @"Failed to create a group, please operate again")];
            }
        } onQueue:nil];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

@end
